//
//  ViewController.m
//  TomCat
//
//  Created by @Zs on 15/6/28.
//  Copyright (c) 2015年 @Zs. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *bgImage;

@end

@implementation ViewController



- (void)setAnimationWithName:(NSString *)imgName andImageCount:(int)imgCount{

    NSMutableArray *array = [NSMutableArray array];
    
    
    
    if(self.bgImage.isAnimating)return;//当前动画如果在执行就不能再点击
    
    
    
    for (int i = 0; i<imgCount; i++) {
        
        NSBundle *bundle = [NSBundle mainBundle];
        NSString *fileName = [NSString stringWithFormat:@"%@_%02d",imgName,i];
        NSString *filePath = [bundle pathForResource:fileName ofType:@"jpg"];
        UIImage *fileImage = [UIImage imageWithContentsOfFile:filePath];
        [array addObject:fileImage];
        
    }
    
    self.bgImage.animationImages = array;
    self.bgImage.animationDuration = array.count*0.1;
    self.bgImage.animationRepeatCount = 1;
    
    [self.bgImage startAnimating];
    [self.bgImage performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.bgImage.animationDuration];//释放内存
    
    

}

/**
 *  清除缓存
 */
- (void)clearImageTomcat{
    
    self.bgImage.animationImages = nil;
    
}



/**
 *  爆头
 */
- (IBAction)touchHead {
    
    [self setAnimationWithName:@"knockout" andImageCount:81];
    
}

/**
 *  喝水
 */
- (IBAction)drink {
   
    [self setAnimationWithName:@"drink" andImageCount:81];
    
}


/**
 *  放屁
 */
- (IBAction)fart {
    [self setAnimationWithName:@"fart" andImageCount:28];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
